package com.byteshake.ddoffice.ui.login.vm

import android.util.Log
import androidx.lifecycle.viewModelScope
import com.byteshake.common.base.BaseViewModel
import com.byteshake.common.base.IUiIntent
import com.byteshake.ddoffice.model.respository.HomeRepository
import com.byteshake.ddoffice.model.respository.LoginRepository
import kotlinx.coroutines.launch

class LoginViewModel : BaseViewModel<LoginState, LoginIntent>() {

    private val loginRepo: LoginRepository = LoginRepository()

    override fun initUiState(): LoginState = LoginState(LoginLoginState.INIT, RegisterState.INIT, SendCodeState.INIT)
    private val TAG = "LoginViewModel"
    override fun handleIntent(intent: IUiIntent) {
        Log.e(TAG, "handleIntent: $intent")
//        viewModelScope.launch {
//            Log.e(TAG, "handleIntent------>>> ${HomeRepository().requestWanData()}" )
//        }
//        viewModelScope.launch {
//            val aa = intent as LoginIntent.Login
//            loginRepo.requestLogin(aa.mobile, 86, aa.password)
//        }

        when (intent){
            is LoginIntent.Login ->{
                requestDataWithFlow(
                    showLoading = true,
                    request = { loginRepo.requestLogin(intent.mobile, 86, intent.password) },
                    successCallback = { data ->
                        Log.e(TAG, "successCallback: $data")
                        sendUiState {
                            copy(
                                loginLoginState = LoginLoginState.SUCCESS(data)
                            )
                        }
                    },
                    failCallback = {
                        Log.e(TAG, "failCallback: $it")
                    }
                )
            }
            is LoginIntent.Register ->{

            }
            is LoginIntent.SendCode ->{

            }
        }
    }

}